package com.lx.javasunmmrize.thread;

/**
 * @Description 经典的线程并发案例01:买票
 *      结果显示多出了几张票,典型的线程安全问题
 * @Author lx
 * @Date 2021/3/1 21:42
 **/
public class ThreadProblem01 implements Runnable{

    //定义票
    private int tockit = 10;

    @Override
    public   void run() {
//        for (int i = 0; i < 10; i++) {
//            System.out.println(Thread.currentThread().getName()+" 拿到了第"+tockit--+"张票");
//        }
//
//        System.out.println("进入run方法");
        while (true){
            if (tockit<=0){
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName()+" 拿到了第"+tockit--+"张票");
        }
    }

    public static void main(String[] args) {
        ThreadProblem01 th = new ThreadProblem01();
        new Thread(th,"小明").start();
        new Thread(th,"校长").start();
        new Thread(th,"某某").start();
    }
}
